package com.shopoo.behavior.mobile;

import com.shopoo.behavior.api.LikeService;
import com.shopoo.behavior.dto.clientobject.BehaviorLikeCO;
import com.shopoo.behavior.dto.cqe.BehaviorLike;
import com.shopoo.behavior.dto.cqe.BehaviorLikeAddCmd;
import com.shopoo.behavior.dto.cqe.BehaviorLikeCancelCmd;
import com.szmengran.cola.dto.SingleResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description: 点赞服务
 * @Package com.szmengran.behavior.controller
 * @CreateTime Mar 4, 2019 2:11:58 PM
 * @Author <a href="mailto:android_li@sina.cn">Joe</a>
 */
@Tag(name = "点赞服务API")
@RestController
@RequestMapping(path = "/like", produces = { "application/json" })
public class LikeController {

    private static final ExecutorService executor = Executors.newFixedThreadPool(100);

    @Resource
    private LikeService likeService;
    
    /**
     * 检查用户是否点赞了分享人的链接
     * @param promotionid
     * @param shareuserid
     * @param openid
     * @return
     * @throws Exception
     */
    @Operation(summary = "检查是否已经点赞")
    @GetMapping("/{promotionid}/{shareuserid}/{openid}")
    public SingleResponse<BehaviorLikeCO> checkLike(@PathVariable("promotionid") String promotionid,
                                                    @PathVariable("shareuserid") String shareuserid, @PathVariable("openid") String openid) {
        BehaviorLike behaviorLike = BehaviorLike
                .builder()
                .promotionid(promotionid)
                .shareuserid(shareuserid)
                .openid(openid)
                .build();
        return likeService.findById(behaviorLike);
    }

    /**
     * 点赞
     * 
     * @param behaviorLikeAddCmd
     * @return
     * @throws Exception
     * @author <a href="mailto:android_li@sina.cn">Joe</a>
     */
    @Operation(summary = "点赞")
    @PostMapping
    public SingleResponse<Boolean> addLike(@RequestBody @Parameter(name = "点赞实体对象", description = "传入json格式", required = true) BehaviorLikeAddCmd behaviorLikeAddCmd) {
        return likeService.like(behaviorLikeAddCmd);
    }

    /**
     * 取消点赞
     * 
     * @param behaviorLikeCancelCmd
     * @return
     * @throws Exception
     * @author <a href="mailto:android_li@sina.cn">Joe</a>
     */
    @Operation(summary = "取消点赞")
    @DeleteMapping
    public SingleResponse<Boolean> cancelLike(@RequestBody @Parameter(name = "点赞实体对象", description = "传入json格式", required = true) BehaviorLikeCancelCmd behaviorLikeCancelCmd) {
        return likeService.cancel(behaviorLikeCancelCmd);
    }

    /**
     * 获取点赞数量
     * 
     * @param promotionid
     * @param shareuserid
     * @return
     * @throws Exception
     * @author <a href="mailto:android_li@sina.cn">Joe</a>
     */
    @Operation(summary = "获取点赞数量")
    @GetMapping("/count/{promotionid}/{shareuserid}")
    public SingleResponse<Integer> getLike(@PathVariable("promotionid") String promotionid,
            @PathVariable("shareuserid") String shareuserid) {
        return likeService.getLike(promotionid, shareuserid);
    }
}
